angular.module('Controller')
.controller('QgyEditConfigureModalCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    $scope.type=type=items.type
    $scope.data=data=items.data
    $scope.modalTitle=if type=='edit' then '配置房间标配' else '查看房间标配'
    $scope.new={}
    loadType=()->
        $scope.configureTypeList=[]
        $http.get("/dictionary/key/configure_type")
        .success((resp)->
            if resp.code==0
                $scope.configureTypeList=resp.message.dictionaryList
                if $scope.new.type == undefined and $scope.configureTypeList.length > 0
                    $scope.new.type = $scope.configureTypeList[0].code
        )
    loadType()

    $scope.modalTableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            configureId:data.id
        }
        $http.get('/configure',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    $scope.addDevice=()->
        reqData=angular.copy($scope.new)
        $http.post("/configure",reqData)
        .success((resp)->
            if resp.code==0
                $http.post("/configureType/#{data.id}/device?device="+resp.message)
                .success((resp)->
                    if resp.code==0
                        wfAlert.toastInfo('添加设备成功')
                        $scope.modalTableParams.reload()
                        $scope.new={}
                        $scope.new.type=$scope.configureTypeList[0].code if $scope.configureTypeList.length>0
                    else
                        wfAlert.toastError('添加设备失败')
                )
            else
                wfAlert.toastError('添加设备失败')
        )
    $scope.removeDevice=(item)->
        $http.delete("/configureType/#{data.id}/device?device="+item.id)
        .success((resp)->
            if resp.code==0
                wfAlert.toastInfo('移除设备成功')
                $scope.modalTableParams.reload()
            else
                wfAlert.toastError(resp.message)
        )
    $scope.confirm = ()->
        $uibModalInstance.dismiss('cancel')
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)